package com.adam.example.util;

import com.squareup.okhttp.*;

import java.io.IOException;

public class OkHttpUtil {

    public static String postSearch(String index, String requestBodyJson) throws IOException {
        Request request = new Request.Builder()
                .url("http://192.168.0.112:9200/" + index + "/_search")
                .addHeader("Authorization", "Basic ZWxhc3RpYzplbGFzdGlj")
                .post(RequestBody.create(MediaType.parse("application/json"), requestBodyJson))
                .build();
        OkHttpClient client = new OkHttpClient();
        Call call = client.newCall(request);
        Response response = call.execute();
        int code = response.code();
        if(code != 200) {
            System.err.println("响应码: " + code);
        }
        ResponseBody responseBody = response.body();
        String responseBodyJson = responseBody.string();
        responseBody.close();
        return responseBodyJson;
    }

}
